﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.IO;

namespace RattlerRace
{
    /// <summary>
    /// AUTHORS Tadej Gruber
    /// </summary>
    public class XmlHandler
    {
        TextWriter sw;
        TextReader sr;

        XmlSerializer xs;
        XmlSerializerNamespaces xsn;

        public XmlHandler()
        {
            xsn = new XmlSerializerNamespaces();
            xsn.Add("", "");
        }

        /// <summary>
        /// Funkcija, ki shrani objekt scoreboarda v xml datoteko s pomočjo serializerja
        /// </summary>
        /// <param name="score"></param>
        public void saveToXml(Scoreboard score)
        {
            sw = new StreamWriter("../../scores.xml", false);
            xs = new XmlSerializer(score.GetType(), "");
            xs.Serialize(sw, score, xsn);
            sw.Close();
        }

        /// <summary>
        /// Funkcija, ki bere xml datoteko s shranjenimi točkami in vrne objekt scoreboard z vsemi napolnjenimi vrednostmi
        /// </summary>
        /// <returns></returns>
        public Scoreboard readFromXml()
        {
            Scoreboard score = new Scoreboard();

            sr = new StreamReader("../../scores.xml");
            xs = new XmlSerializer(score.GetType());
            score = (Scoreboard)xs.Deserialize(sr);
            sr.Close();

            return score;

        }
    }
}
